package com.home.jinlanhua.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.home.jinlanhua.constant.AllCode;
import com.home.jinlanhua.constant.Constants;
import com.home.jinlanhua.dao.UserDao;
import com.home.jinlanhua.entity.User;
import com.home.jinlanhua.exception.CastException;
import com.home.jinlanhua.service.IUserService;
import com.home.jinlanhua.utils.DESUtils;
import com.home.jinlanhua.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import io.jsonwebtoken.*;
import java.util.Date;

import java.util.HashMap;

import static com.alibaba.druid.util.Utils.md5;

/**
 * @program: jinlanhua
 * @description:
 * @author: Mr.Wang
 * @create: 2020-07-06 14:19
 **/
@Service
public class UserService implements IUserService {

    @Autowired
    private UserDao userDao;

    @Override
    public HashMap<String, String> login(User user) {
        // 查找用户名
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username",user.getUsername());
        User userByUsername = userDao.selectOne(objectQueryWrapper);
        //  找到了用户
        if (userByUsername != null) {
            //      验证用户密码是否正确
            if (userByUsername.getPassword().equals(md5(md5(user.getPassword())))) {
                //          用户密码正确
                //              返回生成的jwt的token码给前端
                return getTokenResult(userByUsername);
            }else {
                //          用户密码错误
                //              返回用户密码错误，请重新输入
                CastException.cast(AllCode.PASSWORD_ERROR);
            }
        }
        //  没有找到用户，
        else {
            //      返回用户不存在
            CastException.cast(AllCode.USER_NOTEXIT);
        }
        // 返回错误
        return null;
    }

    /**
     * 生成token
     * @param loginUser
     * @return
     */
    private HashMap<String, String> getTokenResult(User loginUser){
        String token = null;
        try {
            token = JwtUtil.createJWT(loginUser.getUsername(), loginUser.getPassword(),24*60*60);
            HashMap<String, String> result = new HashMap<>();
            result.put("token", token);
            result.put("id", loginUser.getId());
            result.put("realname", loginUser.getRealname());
            result.put("username", loginUser.getUsername());
            result.put("phone", loginUser.getPhone());
            result.put("email", loginUser.getEmail());
            result.put("user_avatar", loginUser.getUserAvatar());
            if (StringUtils.isEmpty(loginUser.getPassword())) {
                result.put("passwordType", "0");
            } else {
                result.put("passwordType", "1");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
